//
//  ChipXiaoXiDetailViewController.m
//  CarPark
//
//  Created by oceans on 2019/8/26.
//  Copyright © 2019 oceans. All rights reserved.
//

#import "ChipXiaoXiDetailViewController.h"
#import "ChipInfoCollectionViewCell.h"

@interface ChipXiaoXiDetailViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) UICollectionView *collection;
@property (strong, nonatomic) NSDictionary *info;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UILabel *dateLabel;
@end

@implementation ChipXiaoXiDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupView];
    
    [self loadData];
}

- (void)setupView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"详情";
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, XNavigationHeight+5.0, ScreenWidth, 20.0)];
    _titleLabel.font = [UIFont systemFontOfSize:14.0];
    _titleLabel.textColor = [UIColor blackColor];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_titleLabel];
    
    self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_titleLabel.frame)+5.0, ScreenWidth, 15.0)];
    _dateLabel.font = [UIFont systemFontOfSize:11.0];
    _dateLabel.textColor = COLOR(153, 153, 153, 1);
    _dateLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_dateLabel];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, XNavigationHeight+50.0, ScreenWidth, ScreenHeight-XNavigationHeight-50.0) collectionViewLayout:layout];
    _collection.backgroundColor = [UIColor whiteColor];
    _collection.delegate = self;
    _collection.dataSource = self;
    [self.view addSubview:_collection];
    [_collection registerNib:[UINib nibWithNibName:@"ChipInfoCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ChipInfoCollectionViewCell"];
}

- (void)loadData {
    
    defweakself
    [HttpRequest getURL:[NSString stringWithFormat:knotifi_chip_detail, __id] paras:nil showProgress:NO successful:^(id  _Nullable result) {
        NSLog(@"%@", result);
        weakself.titleLabel.text = result[@"datas"][@"title"];
        weakself.dateLabel.text = result[@"datas"][@"created_at"];
        weakself.info = result[@"datas"];
        [weakself.collection reloadData];
    } failed:^(int code, NSString * _Nullable errMsg) {
        
    }];
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0.0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(ScreenWidth, 40.0);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [(NSArray *)_info[@"autocars_datas"] count]+1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ChipInfoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ChipInfoCollectionViewCell" forIndexPath:indexPath];
    if (indexPath.item == 0) {
        cell.backgroundColor = COLOR(153, 153, 153, 1);
        cell.label1.text = [NSString stringWithFormat:@"芯片号"];
        cell.label2.text = [NSString stringWithFormat:@"车辆名称"];
        cell.label3.text = [NSString stringWithFormat:@"车架号"];
    }else {
        cell.backgroundColor = [UIColor whiteColor];
        NSDictionary *dic = _info[@"autocars_datas"][indexPath.item-1];
        cell.label1.text = [NSString stringWithFormat:@"%@", dic[@"rfid_id"]];
        cell.label2.text = [NSString stringWithFormat:@"%@", dic[@"car_model"]];
        cell.label3.text = [NSString stringWithFormat:@"%@", dic[@"vin_no"]];
    }
    return cell;
}

@end
